﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Microsoft.Win32;
using System.Drawing.Text;
using Mengsk.ProxyDelivery.Print.Domain;
using Mengsk.ProxyDelivery.Print.ViewModels;
using Mengsk.ProxyDelivery.Domain;


namespace Mengsk.ProxyDelivery.Print.UI
{
    /// <summary>
    /// Interaction logic for DeliveryTemplateConfig.xaml
    /// </summary>
    public partial class PrintTemplateUserControl : UserControl
    {
        private FilePrintTemplateRepertory deliveryTemplateRepertory = new FilePrintTemplateRepertory();
        private ObservableCollection<PrintTemplate> deliveryTemplates = new ObservableCollection<PrintTemplate>();
        private bool loaded = false;
        PrintDialog pd = new PrintDialog();

        public PrintTemplateUserControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (loaded)
            {
                return;
            }
            this.cbbDeliverCompanies.ItemsSource = PrintTemplateCompanyHelper.GetPrintSupport();
            this.lstDeliveryPrintTemplateTypes.ItemsSource = ViewModels.PrintTemplateItemTypeViewModel.GetAllTypes();
            this.cbbDeliveryPrintTemplateItemFontName.ItemsSource = new InstalledFontCollection().Families.Select(obj => obj.Name).ToArray();
            this.cbbDeliveryPrintTemplateItemFontName.SelectedIndex = 0;
            this.lstDeliveryPrintTemplates.ItemsSource = deliveryTemplates;
            this.btnRefreshAllDeliveryPrintTemplate_Click(null, null);
            loaded = true;
        }

        private void AttachThumbEvents(PrintTemplateItemViewModelCommon thumb)
        {
            thumb.PreviewMouseLeftButtonDown += thumb_MouseEnter;
        }

        private void DeattachThumbEvents(PrintTemplateItemViewModelCommon thumb)
        {
            thumb.PreviewMouseLeftButtonDown -= thumb_MouseEnter;
        }

        void thumb_MouseEnter(object sender, MouseEventArgs e)
        {
            this.SetDeliveryPrintTemplateItemProperties(sender as PrintTemplateItemViewModelCommon);
            (sender as PrintTemplateItemViewModelCommon).UI.Focus();
        }

        private void SetDeliveryPrintTemplateItemProperties(PrintTemplateItemViewModelCommon itemBrige)
        {
            this.dpExtendUI.Children.Clear();
            foreach (FrameworkElement item in this.dgDelivertyTemplateItemProperties.Children)
            {
                item.DataContext = itemBrige;
            }
            if (itemBrige != null)
            {
                this.cbbDeliveryPrintTemplateItemFontName.SelectedItem = itemBrige.FontName;
                this.tbDeliveryPrintTemplateItemFontSize.Text = itemBrige.FontSize.ToString();
                if (itemBrige.PropertyUI != null)
                    this.dpExtendUI.Children.Add(itemBrige.PropertyUI);
            }
        }

        private void tbNewDeliveryPrintTemplate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string name = this.tbNewDeliverPrintTemplateName.Text.Trim();
                if (string.IsNullOrWhiteSpace(name))
                {
                    MessageBox.Show("模板名称不能为空");
                    return;
                }

                if (this.deliveryTemplates.Any(obj => obj.Name == name))
                {
                    MessageBox.Show("已存在相同的模板名称");
                    return;
                }

                PrintTemplate deliveryTemplate = new PrintTemplate { Name = name };
                deliveryTemplateRepertory.Insert(deliveryTemplate);
                this.deliveryTemplates.Add(deliveryTemplate);
                this.lstDeliveryPrintTemplates.SelectedItem = deliveryTemplate;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "添加失败");
            }
        }

        private void btnDeleteDeliveryPrintTemplate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrintTemplate deliveryTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;

                if (deliveryTemplate == null)
                {
                    MessageBox.Show("请先选择一个模板");
                    return;
                }

                if (MessageBox.Show("是否要删除：" + deliveryTemplate.Name + "　？", "警告", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }

                //this.deliveryTemplateRepertory.Delete(deliveryTemplate.Name);
                this.deliveryTemplates.Remove(deliveryTemplate);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "删除失败");
            }
        }

        private void btnRefreshAllDeliveryPrintTemplate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var items = this.deliveryTemplateRepertory.GetAll();
                this.deliveryTemplates.Clear();
                foreach (var item in items)
                {
                    this.deliveryTemplates.Add(item);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "获取所有模板失败");
            }
        }

        private void btnSaveDeliveryPrintTemplate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrintTemplate deliveryTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                if (deliveryTemplate == null)
                {
                    MessageBox.Show("请选择相应模板");
                    return;
                }

                string delivertyTemplateName = this.tbDeliveryTemplateName.Text.Trim();
                deliveryTemplate.DeliveryCompany = this.cbbDeliverCompanies.SelectedItem as PrintTemplateCompany;
                deliveryTemplate.Width = this.imgDelivery.Width;
                deliveryTemplate.Height = this.imgDelivery.Height;
                deliveryTemplate.PaperType = this.cbbPaperType.SelectedIndex == 0 ? PaperType.NORMAL : PaperType.HOT;
                if (deliveryTemplate.DeliveryCompany == null)
                {
                    throw new Exception("必须选择快递公司");
                }
                if (deliveryTemplate.Width <= 0 || deliveryTemplate.Height <= 0)
                {
                    throw new Exception("长与宽必须大于0");
                }
                this.deliveryTemplateRepertory.Update(deliveryTemplate, this.tbDeliveryTemplateName.Text.Trim());
                this.lstDeliveryPrintTemplates.ItemsSource = null;
                this.lstDeliveryPrintTemplates.ItemsSource = this.deliveryTemplates;
                this.lstDeliveryPrintTemplates.SelectedItem = deliveryTemplate;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误");
            }
        }

        private void btnChooseFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();

            if (ofd.ShowDialog().Value == false)
            {
                return;
            }
            try
            {
                var item = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                if (item == null)
                {
                    return;
                }
                item.Image = System.IO.File.ReadAllBytes(ofd.FileName);
                this.SetDeliverPrintTemplateBackgroundImage(item.Image);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 设置背景图片
        /// </summary>
        /// <param name="imageBytes">图片数据</param>
        /// <param name="targetWidth"></param>
        /// <param name="targetHeight"></param>
        private void SetDeliverPrintTemplateBackgroundImage(byte[] imageBytes, double targetWidth = 0, double targetHeight = 0)
        {
            //如果没有图片则设置为空
            if (imageBytes == null || imageBytes.Length == 0)
            {
                this.imgDelivery.Source = null;
                this.cDeliveryHost.LayoutTransform = null;
                this.imgDelivery.Height = targetHeight;
                this.imgDelivery.Width = targetWidth;
                return;
            }
            var image = new BitmapImage();
            image.BeginInit();
            image.StreamSource = new MemoryStream(imageBytes, false);
            image.EndInit();
            this.imgDelivery.Source = image;
            this.imgDelivery.Height = targetHeight > 0 ? targetHeight : image.Height;
            this.imgDelivery.Width = targetWidth > 0 ? targetWidth : image.Width;

            //计算缩放
            int physicalWidth = 0, physicalHeight = 0, pixelWidth = 0, pixelHeight = 0;
            if (Win32.Monitor.GetMonitorInfo(this, ref physicalWidth, ref physicalHeight, ref pixelWidth, ref pixelHeight) == false)
            {
                MessageBox.Show("无法获取显示器信息,你看到的图片可能与实际大小不一致,但不影响打印");
                this.cDeliveryHost.LayoutTransform = null;
                return;
            }
            PresentationSource source = PresentationSource.FromVisual(this);
            double realDpiX = pixelWidth / (physicalWidth / 2.54);
            double scaleX = realDpiX / (source.CompositionTarget.TransformToDevice.M11 * 96);

            double realDpiY = pixelHeight / (physicalHeight / 2.54);
            double scaleY = realDpiY / (source.CompositionTarget.TransformToDevice.M22 * 96);

            this.cDeliveryHost.LayoutTransform = new ScaleTransform(scaleX, scaleY);
        }

        /// <summary>
        /// 选择模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstDeliveryTemplates_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                //清空界面
                this.SetDeliveryPrintTemplateItemProperties(null);
                this.imgDelivery.Source = null;
                foreach (var thumb in this.cDeliveryHost.Children.OfType<Thumb>().ToArray())
                {
                    this.cDeliveryHost.Children.Remove(thumb);
                }

                var deliveryTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                this.dgDeliverPrintTemplateHolder.IsEnabled = deliveryTemplate != null;
                if (deliveryTemplate == null)
                {
                    return;
                }

                if (deliveryTemplate.PaperType == PaperType.HOT)
                {
                    this.cbbPaperType.SelectedIndex = 1;
                }
                else
                {
                    this.cbbPaperType.SelectedIndex = 0;
                }

                this.SetDeliverPrintTemplateBackgroundImage(deliveryTemplate.Image, deliveryTemplate.Width, deliveryTemplate.Height);

                //生成视图对象
                PrintTemplateItemViewModelCommon[] briges = deliveryTemplate.Items.Select(obj => PrintTemplateItemViewModelFactory.Create(deliveryTemplate, obj.Type, obj.Title)).ToArray();
                for (int i = 0; i < briges.Length; i++)
                {
                    briges[i].ApplayStyleAndData(this.FindResource("ThumbStyle") as Style, deliveryTemplate.Items[i]);
                    AttachThumbEvents(briges[i]);
                }
                foreach (var thumb in briges)
                {
                    this.cDeliveryHost.Children.Add(thumb.UI);
                }

                var layer = AdornerLayer.GetAdornerLayer(this.cDeliveryHost);
                foreach (UIElement ui in cDeliveryHost.Children.OfType<Thumb>())
                    layer.Add(new PrintTemplateItemAdorner(ui));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "错误");
            }
        }

        /// <summary>
        /// 点击增加模板中的打印项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeliveryPrintTemplateItemType_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FrameworkElement cb = sender as FrameworkElement;
                if (cb == null)
                {
                    return;
                }
                var deliveryTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                PrintTemplateItemTypeViewModel vmItem = cb.DataContext as PrintTemplateItemTypeViewModel;
                PrintTemplateItemViewModelCommon itemViewModel = PrintTemplateItemViewModelFactory.Create(deliveryTemplate, vmItem.Type, vmItem.Title);
                PrintTemplateItem item = new PrintTemplateItem();
                item.Id = Guid.NewGuid();
                item.Type = vmItem.Type;
                item.Title = vmItem.Title;
                item.X = (int)this.imgDelivery.Width / 2;
                item.Y = (int)this.imgDelivery.Height / 2;
                item.Width = 100;
                item.Height = 30;
                item.FontSize = 12;
                item.FontName = "黑体";
                item.Format = itemViewModel.Format;
                item.Value = itemViewModel.Value;
                item.Value1 = itemViewModel.Value1;
                itemViewModel.ApplayStyleAndData(this.FindResource("ThumbStyle") as Style, item);
                AttachThumbEvents(itemViewModel);
                //添加到界面
                this.cDeliveryHost.Children.Add(itemViewModel.UI);
                var layer = AdornerLayer.GetAdornerLayer(this.cDeliveryHost);
                layer.Add(new PrintTemplateItemAdorner(itemViewModel.UI));

                deliveryTemplate.Items.Add(item);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 删除模板中的打印项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRemoveDeliveryPrintTemplateItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FrameworkElement cb = sender as FrameworkElement;
                if (cb == null)
                {
                    return;
                }
                var brige = cb.DataContext as PrintTemplateItemViewModelCommon;
                DeattachThumbEvents(brige);
                this.cDeliveryHost.Children.Remove(brige.UI);
                var deliveryTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                deliveryTemplate.Items.Remove(brige.Data);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        /// <summary>
        /// 打印测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrintTest_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrintTemplate printTemplate = this.lstDeliveryPrintTemplates.SelectedItem as PrintTemplate;
                if (printTemplate == null)
                {
                    MessageBox.Show("请选择一个模板");
                    return;
                }

                int count = 0;
                if (int.TryParse(this.tbTestCount.Text.Trim(), out count) == false)
                {
                    MessageBox.Show("请输入要打印测试的数量");
                    return;
                }

                bool? ret = pd.ShowDialog();

                if (ret == null || ret.Value == false)
                {
                    return;
                }

                pd.PrintTicket.PageMediaSize = new System.Printing.PageMediaSize(printTemplate.Width, printTemplate.Height);

                Order[] orders = new Order[count];
                for (int i = 0; i < count; i++)
                {
                    orders[i] = this.CreateTestOrder();
                }
                OrderPrintDocument doc = new OrderPrintDocument(orders, printTemplate);
                pd.PrintDocument(doc, "打印测试");
                MessageBox.Show("打印完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 创建测试订单
        /// </summary>
        /// <returns></returns>
        private Order CreateTestOrder()
        {
            Order order = new Order
            {
                BuyerComment = "请仔细检查质量,质量不过关不要",
                CreateTime = DateTime.Now,
                DeliveryCompany = "圆通速递",
                DeliveryNumber = "D00099991111",
                DeliveryOperator = "1008",
                DeliveryTime = DateTime.Now,
                Id = 201590890,
                MarkPopDeliveryTime = DateTime.Now,
                OrderGoodss = null,
                PayTime = DateTime.Now,
                PopBuyerId = "821234536",
                PopOrderId = "543543543-20140908-1234568908",
                PopOrderMoney = 123,
                PopPayType = OrderPopPayType.COD,
                PopShopId = 1,
                PopType = PopType.TAOBAO,
                PrintOperator = "1008",
                PrintTime = DateTime.Now,
                ReceiverAddress = "四川省 成都市 金牛区 成都大道34号金家花园",
                ReceiverMobile = "15882415366",
                ReceiverName = "张三",
                ReceiverPhone = "028-88452365",
                SellerComment = "发顺丰",
                State = OrderState.Payed,
                Weight = 5,
                CloseOperator = "",
                CloseTime = DateTime.Now,
            };
            order.OrderGoodss = new List<OrderGoods>();

            for (int i = 0; i < 2; i++)
            {
                //生成商品
                OrderGoods og = new OrderGoods
                {
                    Color = "天蓝色",
                    Count = 2,
                    Edtion = "升级版",
                    NumberId = 0,
                    Id = 2076969,
                    Image = "",
                    Number = "628-3",
                    OrderId = 0,
                    PopInfo = "",
                    PopPrice = 34,
                    PopUrl = "http://www.baidu.com",
                    Price = 45,
                    Size = "37",
                    State = OrderState.Payed,
                    Vendor = "永利来鞋业",
                };
                order.OrderGoodss.Add(og);
            }
            return order;
        }

    }
}
